package com.itdct.onflow.core.util

import java.awt.*
import java.awt.geom.AffineTransform
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import kotlin.apply
import kotlin.random.Random
import kotlin.text.toCharArray
import kotlin.to

/**
 * @author Zhouwx
 * @date 2025/5/28 10:02:53
 * @version 1.0
 * @description
 */
object CaptchaGenerator {
    // 排除易混淆字符（网页9/11/10的字符池优化）
    private val charPool = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz".toCharArray()
    private val random = kotlin.random.Random(System.currentTimeMillis())

    fun generate(width: Int = 170, height: Int = 75): Pair<String, BufferedImage> {
        val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
        val g2d = image.createGraphics().apply {
            // 随机背景色（网页3/10的实现）
            background = Color(random.nextInt(200, 255), random.nextInt(200, 255), random.nextInt(200, 255))
            clearRect(0, 0, width, height)
        }

        // 绘制干扰元素（网页3/10的增强版）
        kotlin.repeat(20) { drawRandomLine(g2d, width, height) }  // 干扰线
        kotlin.repeat(100) { drawRandomDot(g2d, width, height) }  // 干扰点

        // 生成扭曲字符（网页10的变形逻辑）
        val code = buildString {
            kotlin.repeat(4) {
                val char = charPool[random.nextInt(charPool.size)]
                append(char)
                drawDistortedChar(g2d, char.toString(), it, width, height)
            }
        }

        g2d.dispose()
        return code to image
    }

    private fun drawDistortedChar(g2d: Graphics2D, char: String, index: Int, width: Int, height: Int) {
        val x = (width / 5) * index + 10
        val y = height / 2 + 15

        g2d.apply {
            color = Color(random.nextInt(120), random.nextInt(120), random.nextInt(120))
            font = Font("Arial", Font.BOLD + Font.ITALIC, 30)
            transform = AffineTransform().apply {
                shear(random.nextDouble(-0.3, 0.3), random.nextDouble(-0.1, 0.1)) // 扭曲
                scale(random.nextDouble(0.8, 1.2), random.nextDouble(0.6, 1.3))   // 缩放
                rotate(random.nextDouble(-0.2, 0.2))
            }
            drawString(char, x, y)
        }
    }

    // 辅助方法：绘制干扰线和点（略，参考网页3/10实现）
    private fun drawRandomLine(g2d: Graphics2D, width: Int, height: Int) {
        // 生成随机起点和终点坐标
        val x1 = random.nextInt(width)
        val y1 = random.nextInt(height)
        val x2 = random.nextInt(width)
        val y2 = random.nextInt(height)

        // 随机颜色（含透明度增强干扰效果）[4,7](@ref)
        g2d.color = Color(
            random.nextInt(256),
            random.nextInt(256),
            random.nextInt(256),
            random.nextInt(50, 220) // 透明度控制
        )

        // 设置线条粗细（1-3像素随机）[2,4](@ref)
        g2d.stroke = BasicStroke(random.nextFloat()*6)

        // 绘制干扰线
        g2d.drawLine(x1, y1, x2, y2)
    }

    private fun drawRandomDot(g2d: Graphics2D, width: Int, height: Int) {
        // 生成随机位置
        val x = random.nextInt(width)
        val y = random.nextInt(height)

        // 随机颜色（浅色系干扰点）[2,7](@ref)
        g2d.color = Color(
            random.nextInt(150, 255),
            random.nextInt(150, 255),
            random.nextInt(150, 255),
            random.nextInt(150, 255)
        )

        // 绘制干扰点（随机大小1x1到3x3像素）
        val size = random.nextInt(1, 5)
        g2d.fillRect(x, y, size, size) // 或用drawOval画圆点
    }
}

fun main() {
    kotlin.repeat(50) {
        val (code, image) = CaptchaGenerator.generate()

        // 保存图片到本地
        val outputFile = File("C:\\Tmp\\1\\captcha-${System.currentTimeMillis()}-${Random.nextInt(100000)}.jpg")
        ImageIO.write(image, "jpg", outputFile)

        println("验证码已生成并保存为: $outputFile")
        println("验证码内容为: $code")
    }

}